<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-form-item label="ID" prop="id">
          <el-input
            v-model="queryParams.id"
            placeholder="输入ID或ID范围用-隔开"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.basic_image_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商家状态" prop="published">
        <el-select v-model="queryParams.published" placeholder="请选择商家状态" clearable>
          <el-option
            v-for="dict in dict.type.product_published"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="货源ID" prop="basicId">
        <el-input
          v-model="queryParams.basicId"
          placeholder="请输入货源ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货源SKU" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入货源SKU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大类目" prop="className">
        <el-select v-model="queryParams.className" placeholder="请选择大类目" clearable>
          <el-option
            v-for="dict in dict.type.amazon_us_class_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小类目" prop="productType">
        <el-input
          v-model="queryParams.productType"
          placeholder="请输入小类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货源平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择货源平台" clearable>
          <el-option
            v-for="dict in dict.type.basic_platform"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="OCR状态" prop="ocrStatus">
        <el-select v-model="queryParams.ocrStatus" placeholder="请选择OCR状态" clearable>
          <el-option
            v-for="dict in dict.type.image_ocr_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="修改者" prop="updateBy">
        <el-select v-model="queryParams.updateBy" placeholder="请选择修改者" clearable>
          <el-option
            v-for="dict in dict.type.update_by_name_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片数量" prop="imageCount">
        <el-input
          v-model="queryParams.imageCount"
          placeholder="图片数量大于等于此数值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序方式">
        <el-select v-model="sortForm.sort" placeholder="请选择排序方式" @change="handleSortChange">
          <el-option label="按ID升序" value="id,asc"></el-option>
          <el-option label="按ID降序" value="id,desc"></el-option>
          <el-option label="按修改时间升序" value="updateTime,asc"></el-option>
          <el-option label="按修改时间降序" value="updateTime,desc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleBatchAccess"-->
<!--          v-hasPermi="['basic:image:edit']"-->
<!--        >批量通过-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:image:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatchOffLine_logo"
          v-hasPermi="['basic:image:edit']"
        >批量侵权不上架
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatchOffLine_prod"
          v-hasPermi="['basic:image:edit']"
        >批量违禁品不上架
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:image:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="imageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column width="80" label="ID" align="center" prop="id"/>
      <el-table-column width="120" label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_image_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column width="80" label="商家状态" align="center" prop="published">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_published" :value="scope.row.published"/>
        </template>
      </el-table-column>
      <el-table-column width="120" label="OCR状态" align="center" prop="ocrStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.image_ocr_status" :value="scope.row.ocrStatus"/>
        </template>
      </el-table-column>
      <el-table-column width="80" label="货源ID" align="center" prop="basicId"/>
      <el-table-column width="120" label="货源SKU" align="center" prop="sku"/>
      <el-table-column width="80" label="货源平台" align="center" prop="platform">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_platform" :value="scope.row.platform"/>
        </template>
      </el-table-column>
      <el-table-column width="120" label="大类目" align="center" prop="className">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.amazon_us_class_name" :value="scope.row.className"/>
        </template>
      </el-table-column>
      <el-table-column label="小类目" align="center" prop="productType" />

      <el-table-column show-overflow-tooltip="true" width="250" label="标题" align="center" prop="title"/>
      <el-table-column label="主图" align="center" prop="mainImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.mainImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="图片数量" align="center" prop="imageCount" />
      <el-table-column width="250" show-overflow-tooltip="true" label="ocr识别结果" align="center" prop="ocrResult"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip="true" label="备注" align="center" prop="remark" />
      <el-table-column width="80px" label="修改者" align="center" prop="updateBy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.update_by_name_list" :value="scope.row.updateBy"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:image:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['basic:image:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <BackToTop/>
    <BackToBottom/>
    <!-- 添加或修改商品图片对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <!-- 顶部按钮组 -->
      <div class="top-button-group">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.basic_image_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="OCR状态" prop="ocrStatus">
          <el-select v-model="form.ocrStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.image_ocr_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="ocr识别结果" prop="ocrResult">
          <el-input v-model="form.ocrResult" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="主图" prop="mainImage">
          <image-preview width="100px" height="100px" :src="form.mainImage"/>
          <el-input v-model="form.mainImage" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="尺寸图" prop="otherImageUrl1">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl1"/>
          <el-input v-model="form.otherImageUrl1" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图2" prop="otherImageUrl2">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl2"/>
          <el-input v-model="form.otherImageUrl2" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图3" prop="otherImageUrl3">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl3"/>
          <el-input v-model="form.otherImageUrl3" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图4" prop="otherImageUrl4">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl4"/>
          <el-input v-model="form.otherImageUrl4" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图5" prop="otherImageUrl5">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl5"/>
          <el-input v-model="form.otherImageUrl5" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图6" prop="otherImageUrl6">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl6"/>
          <el-input v-model="form.otherImageUrl6" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图7" prop="otherImageUrl7">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl7"/>
          <el-input v-model="form.otherImageUrl7" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图8" prop="otherImageUrl8">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl8"/>
          <el-input v-model="form.otherImageUrl8" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图9" prop="otherImageUrl9">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl9"/>
          <el-input v-model="form.otherImageUrl9" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图10" prop="otherImageUrl10">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl10"/>
          <el-input v-model="form.otherImageUrl10" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图11" prop="otherImageUrl11">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl11"/>
          <el-input v-model="form.otherImageUrl11" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图12" prop="otherImageUrl12">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl12"/>
          <el-input v-model="form.otherImageUrl12" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图13" prop="otherImageUrl13">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl13"/>
          <el-input v-model="form.otherImageUrl13" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图14" prop="otherImageUrl14">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl14"/>
          <el-input v-model="form.otherImageUrl14" type="input" placeholder=""/>
        </el-form-item>
        <el-form-item label="副图15" prop="otherImageUrl15">
          <image-preview width="100px" height="100px" :src="form.otherImageUrl15"/>
          <el-input v-model="form.otherImageUrl15" type="input" placeholder=""/>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listImage,
  getImage,
  batchOffLine,
  addImage,
  updateImage,
  batchAccess,
  batchOffLine_logo, batchOffLine_prod
} from '@/api/basic/image'
import globalMixins from '@/utils/globalMixins'
export default {
  name: 'Image',
  dicts: ['amazon_us_class_name', 'basic_platform', 'basic_image_status', 'image_ocr_status','update_by_name_list','product_published'],
  data() {
    return {
      pickerOptions: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品图片表格数据
      imageList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      daterangeCreateTime: [],
      // 查询参数
      sortForm: {
        sort: 'id,desc' // 默认值
      },
      queryParams: {
        id:null,
        pageNum: 1,
        pageSize: 20,
        status: null,
        published: null,
        basicId: null,
        sku: null,
        className: null,
        productType: null,
        platform: null,
        createTime: null,
        ocrStatus: null,
        updateBy: null,
        imageCount:null,
        title:null
      },
      // 表单参数
      form: {},
    }
  },

  created() {
    this.pickerOptions = globalMixins.data().pickerOptions
    this.$set(this.queryParams, 'orderByColumn', 'id');
    this.$set(this.queryParams, 'isAsc', 'desc');
    this.getList()
  },
  methods: {
    handleSortChange(value) {
      const [field, order] = value.split(',');
      // 更新查询参数
      this.$set(this.queryParams, 'orderByColumn', field);
      this.$set(this.queryParams, 'isAsc', order);
      // 根据新的查询参数重新获取列表数据
      this.getList();
    },
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params['beginCreateTime'] = this.daterangeCreateTime[0]
        this.queryParams.params['endCreateTime'] = this.daterangeCreateTime[1]
      }
      if (this.queryParams.id && this.queryParams.id.includes('-')) {
        let parts = this.queryParams.id.split('-');
        this.queryParams.params['beginId'] = parts[0];
        this.queryParams.params['endId'] = parts[1];
      }
      listImage(this.queryParams).then(response => {
        this.imageList = response.rows
        //数据进行整理一下显示。
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        published: null,
        basicId: null,
        sku: null,
        className: null,
        productType: null,
        platform: null,
        title: null,
        mainImage: null,
        otherImageUrl1: null,
        otherImageUrl2: null,
        otherImageUrl3: null,
        otherImageUrl4: null,
        otherImageUrl5: null,
        otherImageUrl6: null,
        otherImageUrl7: null,
        otherImageUrl8: null,
        otherImageUrl9: null,
        otherImageUrl10: null,
        otherImageUrl11: null,
        otherImageUrl12: null,
        otherImageUrl13: null,
        otherImageUrl14: null,
        otherImageUrl15: null,
        createTime: null,
        updateTime: null,
        ocrResult: null,
        ocrStatus: null,
        remark: null,
        imageCount:null,
        updateBy: null,
        title:null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 批量通过按钮 */
    handleBatchAccess(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认通过商品图片编号为"' + ids + '"的数据项？').then(function() {
        return batchAccess(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getImage(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品图片'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateImage(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addImage(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 批量侵权不上架 */
    handleBatchOffLine_logo(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认修改不上架商品图片编号为"' + ids + '"的数据项？').then(function() {
        return batchOffLine_logo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
      })
    },
    /** 批量违纪不上架 */
    handleBatchOffLine_prod(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认修改不上架商品图片编号为"' + ids + '"的数据项？').then(function() {
        return batchOffLine_prod(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/image/export', {
        ...this.queryParams
      }, `image_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.top-button-group {
  margin-bottom: 20px; /* 根据需要调整间距 */
  text-align: right; /* 如果你想让按钮靠右对齐 */
}
</style>
